﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;

namespace WXYW
{
    public partial class TFSQDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillFormData();
            }
        }

        private void FillFormData()
        {
            long applicationID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
            TFSQService service = new TFSQService();
            SpecialServiceForm form = service.GetSpecialServiceForm(applicationID);
            if (form.User != null)
            {
                SQRLbl.Text = form.User.ContactPerson;
            }
            CustomerContactLbl.Text = form.ApplicationForm.User.ContactPerson;
            CustomerLbl.Text = form.ApplicationForm.User.FullName;
            CustomerPhoneLbl.Text = form.ApplicationForm.User.UserMobilePhone;
            CustomerEmailLbl.Text = form.ApplicationForm.User.UserEmail;
            JXSLbl.Text = form.SellerName;
            JXSManagerLbl.Text = form.SellerManager;
            JXSPhoneLbl.Text = form.SellerPhone;
            ReasonLbl.Text = form.ApplyReason;
            statusLbl.Text = form.Status;
            applyDateLbl.Text = form.ApplicationDate.HasValue ? form.ApplicationDate.Value.ToString("yyyy-MM-dd") : "";
            defineDateLbl.Text = form.DefineDate.HasValue ? form.DefineDate.Value.ToString("yyyy-MM-dd") : "";
            fillDateLbl.Text = form.FillDate.HasValue ? form.FillDate.Value.ToString("yyyy-MM-dd") : "";

            if (form.TFSQContent.Count > 0)
            {
                fwnrLbl.Text = form.TFSQContent.First().FWContent;
                departmentLbl.Text = form.TFSQContent.First().Department;
                startDateLbl.Text = form.TFSQContent.First().StartDate.ToString("yyyy-MM-dd");
                endDateLbl.Text = form.TFSQContent.First().EndDate.ToString("yyyy-MM-dd");
                costLbl.Text = "￥" + form.TFSQContent.First().Cost.ToString() + "元";
            }


            double totalCost = 0;
            foreach (SpecialServiceProduct serviceProduct in form.SpecialServiceProduct)
            {
                if (!serviceProduct.ApplicationProductReference.IsLoaded)
                {
                    serviceProduct.ApplicationProductReference.Load();
                }

                if (!serviceProduct.ApplicationProduct.QuatationForm.IsLoaded)
                {
                    serviceProduct.ApplicationProduct.QuatationForm.Load();
                }

                foreach (QuatationForm quatationForm in serviceProduct.ApplicationProduct.QuatationForm)
                {
                    totalCost += quatationForm.Cost;
                }
            }
            costLbl.Text = totalCost.ToString() + "元";
            RMALbl.Text = form.ApplicationForm.RunningNumber;

            //ApplicationProductService productService = new ApplicationProductService();
            applicationProductGridView.DataSource = service.GetTFSQProductList(applicationID);
            applicationProductGridView.DataBind();
            if (form.User1 != null)
            {
                csDateTimeLbl.Text = form.User1SignDate.HasValue ? form.User1SignDate.Value.ToString("yyyy-MM-dd") : "";
                csCommentLbl.Text = form.User1SignType + " " + form.User1Comment;
                csrLbl.Text = form.User1.ContactPerson;
            }
            if (form.User2 != null)
            {
                zsDateTimeLbl.Text = form.User2SignDate.HasValue ? form.User2SignDate.Value.ToString("yyyy-MM-dd") : "";
                zsCommentLbl.Text = form.User2SignType + " " + form.User2Comment;
                zsrLbl.Text = form.User2.ContactPerson;
            }
            if (form.User3 != null)
            {
                jqDateLbl.Text = form.User3SignDate.HasValue ? form.User3SignDate.Value.ToString("yyyy-MM-dd") : "";
                jqCommentLbl.Text = form.User3SignType + " " + form.User3Comment;
                jqrLbl.Text = form.User3.ContactPerson;
            }
        }

        protected void applicationProductGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }
    }
}
